package com.utn.tp.integrador.widget;

import com.utn.tp.integrador.R;

import android.app.Activity;
import android.content.ContentResolver;
import android.os.Bundle;
import android.provider.Settings;
import android.view.WindowManager;

public class Brillo extends Activity {
	private static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
	private static final int SCREEN_MODE_MANUAL = 0;
	private static final int SCREEN_MODE_AUTO = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dummy);
		toggleBrightness();
		finish();
	}

	private void toggleBrightness() {

		try {
			ContentResolver cr = getContentResolver();
			boolean autoBrightOn = (Settings.System.getInt(cr,
					SCREEN_BRIGHTNESS_MODE, -1) == SCREEN_MODE_AUTO);
			if (autoBrightOn) {
				Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE,
						SCREEN_MODE_MANUAL);
			} else {
				Settings.System.putInt(cr, SCREEN_BRIGHTNESS_MODE,
						SCREEN_MODE_AUTO);
			}
			int brightness = Settings.System.getInt(cr,
					Settings.System.SCREEN_BRIGHTNESS);
			Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS,
					brightness);
			WindowManager.LayoutParams lp = getWindow().getAttributes();
			lp.screenBrightness = 100 / 100.0f;
			getWindow().setAttributes(lp);
		} catch (Exception e) {

		}

		final Activity activity = this;
		Thread t = new Thread() {
			public void run() {
				try {
					sleep(500);
				} catch (InterruptedException e) {
				}
				activity.finish();
			}
		};
		t.start();
	}

}
